package org.lyq.erp.process.goods.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.lyq.erp.config.json.JSONResult;
import org.lyq.erp.process.goods.pojo.AddGoodsVo;
import org.lyq.erp.process.goods.pojo.TblWxGoods;
import org.lyq.erp.process.goods.pojo.TblWxGoodsVo;
import org.lyq.erp.process.goods.service.TblWxGoodsService;
import org.lyq.erp.process.goodsku.pojo.TblWxGoodSku;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/goods" })
public class TblWxGoodsController {

	private static Logger logger = LoggerFactory.getLogger(TblWxGoodsController.class);

	@Resource
	private TblWxGoodsService tblWxGoodsService;

	@RequestMapping({ "/gotoAddGood" })
	public String gotoAddGood() {

		return "process/goods/goods";
	}

	@RequestMapping({ "/getGoodsInfo" })
	public String getGoodsInfo(ModelMap map, AddGoodsVo addGoodsVo, String menuUuid) {

		if (addGoodsVo != null && addGoodsVo.getGoodsId() != null) {

			try {

				AddGoodsVo addGoodsVos = tblWxGoodsService.selectGoodsInfo(addGoodsVo);
				if (addGoodsVos == null) {
					addGoodsVo.addErrorMessage("goods015");
					map.put("dataBean", addGoodsVo);
				} else {
					map.put("dataBean", addGoodsVos);
				}
			} catch (Exception e) {

				addGoodsVo.addErrorMessage("goods015");
				map.put("dataBean", addGoodsVo);
			}
		} else {

			addGoodsVo.addErrorMessage("goods015");
			map.put("dataBean", addGoodsVo);
		}
		return "process/goods/goods";
	}

	@RequestMapping({ "/getGoodsSkuInfo" })
	@ResponseBody
	public JSONResult getGoodsSkuInfo(AddGoodsVo addGoodsVo) {
		if (addGoodsVo.getGoodsId() != null) {
			try {

				AddGoodsVo selectGoodsInfo = tblWxGoodsService.selectGoodsInfo(addGoodsVo);
				if (selectGoodsInfo == null) {
					return JSONResult.errorMsg("goods016");
				} else {
					return JSONResult.ok(selectGoodsInfo);
				}

			} catch (Exception e) {
				logger.info(e.getMessage());
				return JSONResult.errorMsg("goods016");
			}
		}
		return JSONResult.errorMsg("goods016");
	}

	@RequestMapping({ "/addOrUpdateGoods" })
	public String addOrUpdateGoods(ModelMap map, AddGoodsVo addGoodsVo, String menuUuid) {

		if (!validationAddGoods(addGoodsVo)) {
			map.put("dataBean", addGoodsVo);
			return "process/goods/goods";
		}
		if (addGoodsVo.getGoodsId() == null) {
			try {
				tblWxGoodsService.insertGoods(addGoodsVo);
			} catch (Exception e) {
				logger.info(e.getMessage());
				addGoodsVo.addErrorMessage("goods005");
				map.put("dataBean", addGoodsVo);
				return "process/goods/goods";
			}
		} else {
			try {
				tblWxGoodsService.updateGoods(addGoodsVo);
			} catch (Exception e) {
				logger.info(e.getMessage());
				addGoodsVo.addErrorMessage("goods0051");
				map.put("dataBean", addGoodsVo);
				return "process/goods/goods";
			}

		}
		return "redirect:/goods/queueList?menuUuid=" + menuUuid;
	}

	@RequestMapping({ "/updadeGoodsSkuPriceInfo" })
	@ResponseBody
	public JSONResult updadeGoodsSkuPriceInfo(AddGoodsVo addGoodsVo) {
		if (addGoodsVo.getGoodsId() != null) {
			try {
				validateGoodsSku(addGoodsVo);
				if (!addGoodsVo.getMap().isEmpty()) {
					return JSONResult.errorMsg("goods014");
				} else {
					tblWxGoodsService.updadeGoodsSkuPriceInfo(addGoodsVo);
					return JSONResult.ok();
				}
			} catch (Exception e) {
				logger.info(e.getMessage());
				return JSONResult.errorMsg("goods016");
			}
		}
		return JSONResult.errorMsg("goods016");
	}

	@RequestMapping({ "/selectNotCheckList" })
	public String selectNotCheckList(ModelMap map, TblWxGoodsVo tblWxGoods) {

		List<TblWxGoods> list = this.tblWxGoodsService.selectNotCheckList();
		tblWxGoods.setGoodslist(list);
		map.put("dataBean", tblWxGoods);

		return "process/goods/goods_check_list";
	}

	@RequestMapping({ "/selectNotSaleList" })
	public String selectNotSaleList(ModelMap map, TblWxGoodsVo tblWxGoods) {

		List<TblWxGoods> list = this.tblWxGoodsService.selectNotSaleList();
		tblWxGoods.setGoodslist(list);
		map.put("dataBean", tblWxGoods);

		return "process/goods/goods_sale_list";
	}

	@RequestMapping({ "/queueList" })
	public String queueList(ModelMap map, TblWxGoodsVo tblWxGoods) {

		List<TblWxGoods> list = this.tblWxGoodsService.selectAllGoods();
		tblWxGoods.setGoodslist(list);
		map.put("dataBean", tblWxGoods);

		return "process/goods/goods_list";
	}

	@RequestMapping({ "/goodsSale" })
	public String goodsSale(ModelMap map, Integer goodsId, String menuUuid, TblWxGoodsVo tblWxGoods) {

		if (goodsId == null) {
			tblWxGoods.addErrorMessage("goods001");
			map.put("dataBean", tblWxGoods);
			return "process/goods/goods_list";
		} else {
			try {
				tblWxGoodsService.goodsSaleById(goodsId);
			} catch (Exception e) {
				e.printStackTrace();
				logger.info(e.getMessage());
				tblWxGoods.addErrorMessage("goods001");
				map.put("dataBean", tblWxGoods);
				return "process/goods/goods_list";
			}
		}
		return "redirect:/goods/selectNotSaleList?menuUuid=" + menuUuid;
	}

	@RequestMapping({ "/goodsNotSale" })
	public String goodsNotSale(ModelMap map, Integer goodsId, String menuUuid, TblWxGoodsVo tblWxGoods) {

		if (goodsId == null) {
			tblWxGoods.addErrorMessage("goods002");
			map.put("dataBean", tblWxGoods);
			return "process/goods/goods_list";
		} else {
			try {
				tblWxGoodsService.goodsNotSaleById(goodsId);
			} catch (Exception e) {
				e.printStackTrace();
				logger.info(e.getMessage());
				tblWxGoods.addErrorMessage("goods002");
				map.put("dataBean", tblWxGoods);
				return "process/goods/goods_list";
			}
		}
		return "redirect:/goods/queueList?menuUuid=" + menuUuid;
	}

	@RequestMapping({ "/goodsCheck" })
	public String goodsCheck(ModelMap map, Integer goodsId, String menuUuid, TblWxGoodsVo tblWxGoods) {

		if (goodsId == null) {
			tblWxGoods.addErrorMessage("goods003");
			map.put("dataBean", tblWxGoods);
			return "process/goods/goods_list";
		} else {
			try {
				tblWxGoodsService.goodsCheckById(goodsId);
			} catch (Exception e) {
				e.printStackTrace();
				logger.info(e.getMessage());
				tblWxGoods.addErrorMessage("goods003");
				map.put("dataBean", tblWxGoods);
				return "process/goods/goods_list";
			}

		}
		return "redirect:/goods/selectNotCheckList?menuUuid=" + menuUuid;
	}

	@RequestMapping({ "/goodsDelete" })
	public String delete(ModelMap map, Integer goodsId, String menuUuid, TblWxGoodsVo tblWxGoods) {

		if (goodsId == null) {
			tblWxGoods.addErrorMessage("goods004");
			map.put("dataBean", tblWxGoods);
			return "process/goods/goods_list";
		} else {
			try {
				tblWxGoodsService.goodsDeleteById(goodsId);
			} catch (Exception e) {
				e.printStackTrace();
				logger.info(e.getMessage());
				tblWxGoods.addErrorMessage("goods004");
				map.put("dataBean", tblWxGoods);
				return "process/goods/goods_list";
			}

		}
		return "redirect:/goods/queueList?menuUuid=" + menuUuid;
	}

	private boolean validationAddGoods(AddGoodsVo addGoodsVo) {

		if (addGoodsVo != null) {

			if (ObjectEmptyUtils.isEmptyString(addGoodsVo.getGoodsImg())) {
				addGoodsVo.addErrorMessage("goods006");
			}

			if (ObjectEmptyUtils.isEmptyString(addGoodsVo.getGoodsName())) {
				addGoodsVo.addErrorMessage("goodsName", "goods007");
			}

			if (ObjectEmptyUtils.isEmptyString(addGoodsVo.getGoodsTips())) {
				addGoodsVo.addErrorMessage("goodsTips", "goods008");
			}

			if (ObjectEmptyUtils.isEmptyString(addGoodsVo.getGoodsNum())) {
				addGoodsVo.addErrorMessage("goodsNum", "goods009");
			}

			if (ObjectEmptyUtils.isEmptyString(addGoodsVo.getGoodsUnit())) {
				addGoodsVo.addErrorMessage("goodsUnit", "goods010");
			}

			if (addGoodsVo.getGoodsWeight() == null) {
				addGoodsVo.addErrorMessage("goodsWeight", "goods011");
			}

			if (isEmptyStrArray(addGoodsVo.getGoodsGallery())) {
				addGoodsVo.addErrorMessage("goods012");
			}

			if (isEmptyStrArray(addGoodsVo.getGoodsDesc())) {
				addGoodsVo.addErrorMessage("goods013");
			}

			validateGoodsSku(addGoodsVo);
		}
		return addGoodsVo.getMap().isEmpty();
	}

	private void validateGoodsSku(AddGoodsVo addGoodsVo) {

		Integer[] skuIds = addGoodsVo.getSkuId();
		String[] goodsSkuNames = addGoodsVo.getGoodsSkuName();
		String[] skuImgs = addGoodsVo.getSkuImg();
		Integer[] goodsSkuStocks = addGoodsVo.getGoodsSkuStock();
		BigDecimal[] goodsSkuPrices = addGoodsVo.getGoodsSkuPrice();
		BigDecimal[] goodsSkuDiscts = addGoodsVo.getGoodsSkuDisct();
		List<TblWxGoodSku> skuList = new ArrayList<TblWxGoodSku>();
		TblWxGoodSku sku = null;

		int totalNum = 0;
		for (int i = 0; i < addGoodsVo.getGoodsSkuDisct().length; i++) {

			sku = new TblWxGoodSku();

			if (!ObjectEmptyUtils.isEmptyArray(skuIds)) {
				if (skuIds[i] == null) {
					addGoodsVo.addErrorMessage("goods014");
				}
			}

			if (!ObjectEmptyUtils.isEmptyArray(goodsSkuNames)) {
				if (ObjectEmptyUtils.isEmptyString(goodsSkuNames[i])) {
					addGoodsVo.addErrorMessage("goods014");
				}
				sku.setGoodsSkuName(goodsSkuNames[i]);
			}

			if (!ObjectEmptyUtils.isEmptyArray(skuImgs)) {
				if (ObjectEmptyUtils.isEmptyString(skuImgs[i])) {
					addGoodsVo.addErrorMessage("goods014");
				}
				sku.setSkuImg(skuImgs[i]);
			}

			if (!ObjectEmptyUtils.isEmptyArray(goodsSkuStocks)) {
				if (goodsSkuStocks[i] == null) {
					addGoodsVo.addErrorMessage("goods014");
				}
				totalNum += goodsSkuStocks[i];
				sku.setGoodsSkuStock(goodsSkuStocks[i]);
			}

			if (goodsSkuPrices[i] == null) {
				addGoodsVo.addErrorMessage("goods014");
			}
			sku.setGoodsSkuPrice(goodsSkuPrices[i]);

			if (goodsSkuDiscts[i] == null) {
				addGoodsVo.addErrorMessage("goods014");
			}
			sku.setGoodsSkuDisct(goodsSkuDiscts[i]);

			if (!ObjectEmptyUtils.isEmptyArray(skuIds)) {
				sku.setSkuId(skuIds[i]);
			}
			skuList.add(sku);
		}
		addGoodsVo.setGoodsStock(totalNum);
		addGoodsVo.setGoodskuList(skuList);
	}

	private boolean isEmptyStrArray(String[] arr) {
		if (arr != null) {
			for (int i = 0; i < arr.length; i++) {
				if (!ObjectEmptyUtils.isEmptyString(arr[i])) {
					return false;
				}
			}
		}
		return true;
	}

}
